package freespiritsocial.io.user.ui.adapter

import android.graphics.Paint
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import freespiritsocial.io.user.R
import freespiritsocial.io.user.data.model.bean.CommentPojo
import freespiritsocial.io.user.databinding.ItemPostCommentsBinding
import freespiritsocial.io.user.ext.init
import freespiritsocial.io.user.ext.setAdapterAnimation
import freespiritsocial.io.user.util.CacheUtil
import freespiritsocial.io.user.viewmodel.PostDetailViewModel

/**
 * 帖子评论
 */
class PostCommentsAdapter(
    val model: PostDetailViewModel,
    data: ArrayList<CommentPojo> = arrayListOf(),
    layoutResIdRes: Int = R.layout.item_post_comments
):
    BaseQuickAdapter<CommentPojo,BaseDataBindingHolder<ItemPostCommentsBinding>>(layoutResIdRes,data),LoadMoreModule {
    val userId by lazy(LazyThreadSafetyMode.NONE) {
        CacheUtil.getUser()?.userId ?: ""
    }
    private val expend: HashSet<Int> = hashSetOf()
    var reply: (Long?) -> Unit = {}
    var goToPeople: (String?) -> Unit = {}

    init {
        setAdapterAnimation(0)
        addChildClickViewIds(R.id.tv_replies_count,R.id.tv_reply,R.id.iv_like,R.id.iv_avatar)
        setOnItemChildClickListener { adapter,view,position ->
            when (view.id) {
                R.id.iv_avatar -> {
                    goToPeople.invoke(data[position].commentUserId)
                }
                R.id.tv_replies_count -> {
                    expend.add(position)
                    notifyItemChanged(position + headerLayoutCount)
                }
                R.id.iv_like -> {
                    val commentPojo = data[position]
                    model.likeComment(commentPojo.commentId ?: return@setOnItemChildClickListener)
                    commentPojo.isLike = commentPojo.isLike?.not()
                    notifyItemChanged(position + headerLayoutCount)
                }
                R.id.tv_reply -> reply.invoke(data[position].commentId)
            }
        }
    }

    override fun convert(holder: BaseDataBindingHolder<ItemPostCommentsBinding>,item: CommentPojo) = with(holder.dataBinding) {
        this?.data = item
        val position = holder.absoluteAdapterPosition - headerLayoutCount
        this?.tvRepliesCount?.paintFlags = Paint.UNDERLINE_TEXT_FLAG
        this?.divider?.isVisible = position != data.lastIndex
        this?.tvReply?.isVisible = item.commentUserId != userId
        this?.recyclerViewSubComments?.let {
            it.isVisible = expend.contains(position)
            if (it.isVisible.not()) return@let
            if (it.adapter == null)
                it.init(PostSubCommentsAdapter(item.replies).apply {
                    addChildClickViewIds(R.id.tv_reply,R.id.iv_like,R.id.iv_avatar)
                    setOnItemChildClickListener { _,view,position ->
                        when (view.id) {
                            R.id.iv_avatar -> {
                                goToPeople.invoke(data[position].commentUserId)
                            }
                            R.id.iv_like -> {
                                val commentPojo = data[position]
                                model.likeComment(commentPojo.commentId ?: return@setOnItemChildClickListener)
                                commentPojo.isLike = commentPojo.isLike?.not()
                                notifyItemChanged(position + headerLayoutCount)
                            }
                            R.id.tv_reply -> reply.invoke(data[position].commentId)
                        }

                    }
                },isScroll = false,hasEmpty = false)
            else {
                val postSubCommentsAdapter = it.adapter as PostSubCommentsAdapter
                postSubCommentsAdapter.setList(item.replies)
            }
        }

        this?.groupExpendComment?.isVisible = item.replies?.isNotEmpty() == true && expend.contains(position).not()
    }
}